﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using VAdvantage.Utility;

namespace VAdvantage.Controls
{
    public partial class ToolBarButton : UserControl, ToolBarItem
    {

        public event RoutedEventHandler Click;

        
        private static ImageSource defaultSource = null;

        private Thickness _iconMargin = new Thickness(3, 0, 0, 0);

        public ToolBarButton()
        {
            defaultSource = Utility.Envs.LoadImageSource("button_bg.png");
            InitializeComponent();
            this.LayoutRoot.MouseLeftButtonUp += OnClick;
            this.LayoutRoot.MouseLeftButtonDown += OnMosueDown;
            this.LayoutRoot.MouseEnter += OnMouseEnter;
            this.LayoutRoot.MouseLeave += OnMouseLeave;
        }

        /// <summary>
        /// Use to Clear Default background of Button
        /// By Karan --
        /// </summary>
        public ImageSource ClearBackGround
        {
            get { return  backgroundImageBrush.ImageSource;}
            set { backgroundImageBrush.ImageSource = value; }
        }


        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            if (_text != null)
            {
                txt.Text = _text;
            }
            if (_imgSource != null)
            {
                ToolItemBrush.Source = _imgSource;
            }
        }


        #region VisualState Members
        private const string MouseOverState = "MouseOver";
        private const string NormalState = "Normal";
        private const string MouseLeaveState = "MouseLeave";

        void OnMouseLeave(object sender, MouseEventArgs e)
        {
            ChangeVisualState(MouseLeaveState);
            // ChangeVisualState("UnSelected");
        }

        void OnMouseEnter(object sender, MouseEventArgs e)
        {

            ChangeVisualState(MouseOverState);
        }

        bool isKeyDownFired = false;
        void OnMosueDown(object sender, MouseEventArgs e)
        {
            isKeyDownFired = true;
            //if (Click != null)
            //ChangeVisualState("Selected");
        }

        void ChangeVisualState(string state)
        {
            VisualStateManager.GoToState(this, state, true);
        }

        #endregion

        void OnClick(object sender, MouseButtonEventArgs e)
        {
            if (!isKeyDownFired)
                return;
            isKeyDownFired = false;
            ChangeVisualState("UnSelected");
            if (Click != null)
            {
                Click(this, new RoutedEventArgs());
            }
        }

        #region ICON
        public ImageSource Icon
        {
            get { return (ImageSource)GetValue(IconProperty); }
            set { SetValue(IconProperty, value); }
        }

        private const string IconPropertyName = "Icon";

        public static readonly DependencyProperty IconProperty = DependencyProperty.Register(IconPropertyName,
           typeof(ImageSource), typeof(ToolBarButton), new PropertyMetadata(null, new PropertyChangedCallback(OnIconPropertyChanged)));

        private static void OnIconPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((ToolBarButton)d).OnIconPropertyChanged(e.NewValue as ImageSource);
        }

        private ImageSource _imgSource = null;
        private void OnIconPropertyChanged(ImageSource newValue)
        {
            if (ToolItemBrush != null && newValue != null)
            {
                ToolItemBrush.Source = newValue;
                _imgSource = null;
            }
            else
            {
                _imgSource = newValue;
            }
        }



        public ImageSource BackgroundImage
        {
            get { return (ImageSource)GetValue(BackgroundImageProperty); }
            set { SetValue(BackgroundImageProperty, value); }
        }

        private const string BackgroundImagePropertyName = "BackgroundImage";

        public static readonly DependencyProperty BackgroundImageProperty = DependencyProperty.Register(BackgroundImagePropertyName,
           typeof(ImageSource), typeof(ToolBarButton), new PropertyMetadata(defaultSource, new PropertyChangedCallback(OnBackgroundImagePropertyChanged)));

        private static void OnBackgroundImagePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((ToolBarButton)d).OnBackgroundImagePropertyChanged(e.NewValue as ImageSource);
        }

        private ImageSource _imgBSource = null;
        private void OnBackgroundImagePropertyChanged(ImageSource newValue)
        {
            if (newValue != null)
            {
                backgroundImageBrush.ImageSource = newValue;
                LayoutRoot.Background = backgroundImageBrush;
                _imgBSource = null;
            }
            else
            {
                LayoutRoot.Background = new SolidColorBrush(Colors.Transparent);
                _imgBSource = newValue;
            }
        }


        public Thickness IconMargin
        {
            get
            {
                return _iconMargin;
            }
            set
            {
                SetMargin(value);
            }
        }

        public double IconHeight
        {
            get
            {
                return ToolItemBrush.Height;
            }
            set
            {
                ToolItemBrush.Height = value;
            }
        }

        public double IconWidth
        {
            get
            {
                return ToolItemBrush.Width;
            }
            set
            {
                ToolItemBrush.Width = value;
            }
        }

        public void SetMargin(Thickness value)
        {
            //string[] margin = value.Split(',');
            _iconMargin = value;
            ToolItemBrush.Margin = _iconMargin;
        }


        #endregion

        #region

        public String Text
        {
            get { return (String)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        private const string TextPropertyName = "Text";

        public static readonly DependencyProperty TextProperty = DependencyProperty.Register(TextPropertyName,
           typeof(String), typeof(ToolBarButton), new PropertyMetadata(null, new PropertyChangedCallback(OnTextPropertyChanged)));

        private static void OnTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((ToolBarButton)d).OnTextPropertyChanged(e.NewValue as String);
        }

        private String _text = null;
        private void OnTextPropertyChanged(String newValue)
        {
            if (txt != null && newValue != null)
            {
                txt.Text = newValue;
                _text = null;

                if (txt.Text == "")
                {
                    txt.Visibility = System.Windows.Visibility.Collapsed;
                    ToolItemBrush.Margin = new Thickness(0);
                }
                else
                {
                    txt.Visibility = System.Windows.Visibility.Visible;
                    ToolItemBrush.Margin = _iconMargin;
                }
            }
            else
            {
                _text = newValue;
            }
        }


        #endregion

        #region "ToolBarItem
        public string ActionName
        {
            get;
            set;
        }

        public bool Enable
        {
            get
            {
                return this.IsEnabled;
            }
            set
            {
                this.IsEnabled = value;
                ChangeVisualState(MouseLeaveState);
                SelectVisual.Visibility = value ? System.Windows.Visibility.Collapsed : System.Windows.Visibility.Visible;


            }
        }

        public bool Visible
        {
            get
            {
                return this.Visibility == System.Windows.Visibility.Visible;
            }
            set
            {
                this.Visibility = value ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
            }
        }


        public bool Pressed
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
        #endregion
    }
}
